package com.example.blew.demo;

import cn.hutool.core.util.StrUtil;
import com.drew.imaging.FileTypeDetector;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.png.PngMetadataReader;
import com.drew.lang.GeoLocation;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.GpsDirectory;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Objects;

/**
 * <p>
 *
 * hutool 测试
 *
 * </p>
 *
 * @author chaodong.zeng@pcitc.com
 * @since 2022/8/16
 */

public class HelloTest {


    public String fun() {
        StrUtil.sub("12",1,1);
        return "hello";
    }


    public static void getGpsDirectory(String filePath){
//        File picFile = getPicFile(filePath);
        File picFile = new File(filePath);
        getGpsDirectory(picFile);
    }
    public static void getGpsDirectory(File picFile){
        Metadata metadata = getPicMetadata(picFile);
        GpsDirectory gpsDirectory = metadata.getFirstDirectoryOfType(GpsDirectory.class);
        if (Objects.nonNull(gpsDirectory)) {
            GeoLocation geoLocation = gpsDirectory.getGeoLocation();
            System.out.println(geoLocation.getLongitude());
            System.out.println(geoLocation.getLatitude());
        }
    }
    public static Metadata getPicMetadata(File picFile){
        Metadata metadata = null;
        try(InputStream inputStream = new FileInputStream(picFile);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) {
            com.drew.imaging.FileType fileType = FileTypeDetector.detectFileType(bufferedInputStream);
            switch (fileType) {
                case Png:
                    metadata = PngMetadataReader.readMetadata(picFile);
                    break;
                case Jpeg:
                    metadata = JpegMetadataReader.readMetadata(picFile);
                    break;
                default:
                    return metadata;
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return metadata;
    }
    public static void main(String[] args) {
        getGpsDirectory("D:\\app\\img\\123ed.jpg");
    }

}